package com.wtf.dsp.admin.service.impl;

import cn.hutool.db.DaoTemplate;
import cn.hutool.jwt.JWTUtil;
import com.wtf.dsp.admin.entity.WtfDspUser;
import com.wtf.dsp.admin.enums.CommonErrorEnum;
import com.wtf.dsp.admin.exception.WtfDspException;
import com.wtf.dsp.admin.mapper.WtfDspUserMapper;
import com.wtf.dsp.admin.model.dto.LoginDTO;
import com.wtf.dsp.admin.model.vo.LoginVO;
import com.wtf.dsp.admin.service.LoginService;
import com.wtf.dsp.admin.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private WtfDspUserMapper wtfDspUserMapper;

    @Resource
    private TokenService tokenService;

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        WtfDspUser wtfDspUser = wtfDspUserMapper.selectById(loginDTO.getUsername());
        if(wtfDspUser == null || !DigestUtils.md5DigestAsHex(loginDTO.getPassword().getBytes()).equals(wtfDspUser.getPassword())){
            throw new WtfDspException(CommonErrorEnum.USERNAME_OR_PASSWORD_ERROR.getErrMsg());
        }
        //使用jwt生成token,设置过期时间
        LoginVO loginVO = new LoginVO();
        String token = tokenService.getToken(loginDTO.getUsername());
        loginVO.setToken(token);
        return loginVO;
    }

    @Override
    public void logout() {
        
    }
}
